#include "StdAfx.h"
#include "TcpManager.h"

float kawasaki[8]={0};
bool hold = false;

TcpManager::TcpManager(String^ host, String^ port)
{
	this->host = host;
	this->port = port;
}

void TcpManager::Connect()
{

	Int32 port_i = Int32::Parse(port);
	client = gcnew TcpClient(host, port_i);


	// Get a client stream for reading and writing.
	stream = client->GetStream();

}


void TcpManager::Accept()
{
	Int32 port_i = Int32::Parse(port);
	IPAddress^ localAddr = IPAddress::Parse(host);

	TcpListener^ server = gcnew TcpListener(localAddr, port_i);

	// Start listening for client requests.
	server->Start();


	// Enter the listening loop.
	while ( true )
	{
		try
		{
			Console::Write( "Waiting for a connection... " );

			// Perform a blocking call to accept requests.
			// You could also user server.AcceptSocket() here.
			TcpClient^ client = server->AcceptTcpClient();
			Console::WriteLine( "Connected!" );

			// Get a stream Object* for reading and writing
			stream = client->GetStream();

			while (SendFrame()>0)
			{			
				Thread::Sleep(125);
			}

			Thread::Sleep(125);
		}
		catch ( IOException^ e)
		{
			Console::WriteLine( "IOException: {0}", e );
		}
	}

}

/*
int TcpManager::Listen()
{
	Int32 i;
				// Buffer for reading data
			array<Byte>^bytes = gcnew array<Byte>(256);
			String^ data = nullptr;
// Loop to receive all the data sent by the client.
					while ( i = stream->Read( bytes, 0, bytes->Length ) )
					{
						
						// Translate data bytes to a ASCII String*.
						data = Text::Encoding::ASCII->GetString( bytes, 0, i );
						array<String^>^ substrings = data->Split('_');
						Console::WriteLine(data);
						//for (int j=0; j<substrings->Length; j++)
						//	Console::WriteLine(substrings[j]);
						
					}
					return 0;
}
*/

int TcpManager::SendFrame()
{
		
	while(hold);

	String^ message = "";
	for (int i=0; i<8; i++)
		message+=kawasaki[i]+"_";

	/// @todo zakomentowalem bo ta funkcja nie jest w pelni funkcjonalna
	// WriteToFile(message);

	array<Byte>^data = Text::Encoding::ASCII->GetBytes( message );
	
	try
	{
		stream->Write(data, 0, data->Length );
		return data->Length;
	}
	catch (System::IO::IOException^ e) 
	{
		Console::WriteLine(e->Message);
		return -1;
	}	

}

void TcpManager::WriteToFile(System::String^ message){
	String^ filepath = "Stream.txt";
	StreamWriter^ sw; 
	if (File::Exists(filepath)){
		/// dla pliku istniejacego:
		sw = gcnew StreamWriter(filepath, true, System::Text::Encoding::Default);
	} else {
		/// dla pliku nieistniejacego:
		sw = gcnew StreamWriter(filepath);
	}
	sw->WriteLine(message);
	sw->Flush();
	sw->Close();
}

void TcpManager::Disconnect()
{

	// Close everything.
	client->Close();
	Console::WriteLine("disconnect");

}

void TcpManager::WaitForKey()
{
	while (true)
	{
		ConsoleKeyInfo c = Console::ReadKey(true);
		switch (c.Key)
		{
			case ConsoleKey::D0:
				kawasaki[0]+=1.5;
				break;			
			case ConsoleKey::D1:
				kawasaki[1]+=5;
				break;
			case ConsoleKey::D2:
				kawasaki[2]+=5;
				break;
			case ConsoleKey::D3:
				kawasaki[3]+=5;
				break;
			case ConsoleKey::D4:
				kawasaki[4]+=5;
				break;
			case ConsoleKey::D5:
				kawasaki[5]+=5;
				break;
			case ConsoleKey::D6:
				kawasaki[6]+=5;
				break;
			case ConsoleKey::D7:
				kawasaki[7]+=5;
				break;

			case ConsoleKey::P:
				kawasaki[0]-=1.5;
				break;
			case ConsoleKey::Q:
				kawasaki[1]-=5;
				break;
			case ConsoleKey::W:
				kawasaki[2]-=5;
				break;
			case ConsoleKey::E:
				kawasaki[3]-=5;
				break;
			case ConsoleKey::R:
				kawasaki[4]-=5;
				break;
			case ConsoleKey::T:
				kawasaki[5]-=5;
				break;
			case ConsoleKey::Y:
				kawasaki[6]-=5;
				break;
			case ConsoleKey::U:
				kawasaki[7]-=5;
				break;

			case ConsoleKey::H:
				if (hold)
					hold=false;
				else
					hold=true;
				
				Console::WriteLine("hold = "+hold);
				break;
		}

		String^ message = "";
		for (int i=0; i<8; i++)
			message+="R["+i+"]="+kawasaki[i]+", ";
		Console::WriteLine(message);

		Thread::Sleep(100);

		// discard input buffer
		while(Console::KeyAvailable) 
		{
			Console::ReadKey(true);
		}

	}
}